// 使用 JvmName 指定编译类名
@file:JvmName("Hero")

package cn.qy.kotlin.base.abc;

import cn.qy.kotlin.base.Jhava
import java.io.IOException

fun main() {
    val adversary = Jhava()
    println(adversary.utterGreeting())
    // Kotlin 编译器认识 Nullable 注解，你就能看到 IntelliJ 关于是否直接在一个 String?类型上调用 toLowerCase 函数的提醒
    val friendshipLevel = adversary.determineFriendshipLevel()
    println(friendshipLevel?.toLowerCase())

    // 直接使用 hitPoints。Kotlin 的这种互操作特性消除了 Kotlin 和 Java 之间的障碍
    val adversaryHitPoints: Int = adversary.hitPoints
    // 变量运行时是原始数据类型 int
    println(adversaryHitPoints.javaClass)
    println(adversaryHitPoints)
    println(adversaryHitPoints.dec())

    adversary.greeting = "Hello, Hero."
    println(adversary.utterGreeting())

    adversary.offerFood()
    try {
        adversary.extendHandInFriendship()
    } catch (e: Exception) {
        println("Begone, foul beast!")
    }
}

fun makeProclamation() = "Greetings, beast!"

@JvmOverloads
fun handOverFood(leftHand: String = "berries", rightHand: String = "beef") {
    println("Mmmm... you hand over some delicious $leftHand and $rightHand.")
}

// 在编写供 Java 开发者调用的 Kotlin API 时，要考虑使用 @Throws 注解
@Throws(IOException::class)
fun acceptApology() {
    throw IOException()
}

val translator = { utterance: String ->
    println(utterance.toLowerCase().capitalize())
}

class Spellbook {
    @JvmField
    val spells = listOf("Magic Ms. L", "Lay on Hans")

    companion object {
        @JvmField
        val MAX_SPELL_COUNT = 10

        // JvmStatic 允许你直接调用伴生对象里的函数
        @JvmStatic
        fun getSpellbookGreeting() = println("I am the Great Grimoire!")
    }
}